package com.yy.satokenapplication.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import lombok.Data;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Data
@Component
@ConfigurationProperties(prefix = "encryption")
public class PasswordEncryptor {

    
    private String key;


    public String encrypt(String password) {
        AES aes = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8));
        return aes.encryptHex(password);
    }

    public String decrypt(String encryptedPassword) {
        AES aes = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8));
        return aes.decryptStr(encryptedPassword);
    }

    public boolean verifyPassword(String inputEncryptedPassword, String storedPassword) {
        String decryptedStoredPassword = decrypt(storedPassword);
        return inputEncryptedPassword.equals(decryptedStoredPassword);
    }

}
